<template>
  <div class="addstaff">
    <p class="addstaff-title">添加员工</p>
    <p class="addstaff-tip"><i class="el-icon-warning" style="color: #B9C0C7" />保存完毕后首次登录请告知员工使用手机号＋验证码方式登录，登录后可自行设置员工账号登录密码</p>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="180px" class="demo-ruleForm">
      <el-form-item label="员工手机号" prop="mobile">
        <el-input v-model="ruleForm.mobile"  class="w240" placeholder="请输入员工手机号" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"  class="w240" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="员工备注名" prop="nickName">
        <el-input v-model="ruleForm.nickName"  class="w240" placeholder="请输入员工备注名" />
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-radio-group v-model="ruleForm.roleId">
          <el-radio v-for="item in roleList" :key="item.roleId" class="mb16" :label="item.roleId" :disabled="item.roleId == -1">{{ item.roleName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-button style="margin-left: 10px" @click="cancelBtn">返回</el-button>
      <el-button type="primary" style="margin-left: 10px" @click="submitForm('ruleForm')">确定</el-button>
    </el-form>
  </div>
</template>
<script>
import { validateMobile, validateEmail } from '@/utils/validate'
import api from '@/api/index'
export default {
  name: 'StaffInfo',
  components: {},
  data() {
    return {
      roleList: [],
      ruleForm: {
        mobile: '',
        email: '',
        nickName: '',
        roleId: '',
        //imListId:'WAMO100370',
        //pathurl:'PtBoss'
        imShowId:this.$store.getters.imShowId,
        imListId:this.$store.getters.imListId,
        pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'twBoss':'PtBoss',
        userName:this.$store.getters.username,
        userId:this.$store.getters.useid
      },
      rules: {
        mobile: [
          { required: true, validator: validateMobile, trigger: 'blur' }
        ],
        email: [
          { required: true, validator: validateEmail, trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入员工备注名', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getQueryAccountRole()
  },
  methods: {
    cancelBtn() {
      const temState = {
        isStaffIndex: true,
        isAddStaff: false,
        isAddAccount: false,
        isEditStaff: false
      }
      this.$emit('templateChange', temState)
    },
    submitForm(formName) {
      let form={
        mobile:this.ruleForm.mobile
      }
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const formdata=  await api.boss.getVipInfo(form)
          debugger
          this.ruleForm.userName=formdata.data.username
          this.ruleForm.userId=formdata.data.userid
          const { data } = await api.boss.addAccountInfo(this.ruleForm)
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.cancelBtn()
        } else {
          return false
        }
      })
    },
    // 获取权限列表
    async getQueryAccountRole() {
      const searchParams ={
            imListId:this.$store.getters.imListId,
            pathurl:this.$store.getters.imShowId.substring(0,2).toLowerCase()=="tw"?'boss':'boss'
      }
      const { data } = await api.boss.queryAccountRoleList(searchParams)
      this.roleList = data
    }
  }
}
</script>
<style lang="scss" scoped>
.addstaff {
  width: 100%;
  height: 100%;
  padding: 16px 16px 16px 32px;
  box-sizing: border-box;
  position: relative;
  overflow-y: auto;
  .addstaff-title {
    font-size: 16px;
    font-weight: bold;
    line-height: 1.5;
    color: #202933;
    padding-left: 8px;
    position: relative;
    &::before {
      content: "";
      width: 2px;
      height: 14px;
      background: #0C7FF2;
      position: absolute;
      left: 0;
      top: 50%;
      margin-top: -7px;
    }
  }
  .addstaff-tip {
    font-size: 12px;
    color: #7E8790;
    line-height: 1.5;
    margin: 4px auto 20px;
    i {
      margin-left: 5px;
    }
  }
  ::v-deep .el-form {
    .el-form-item {
      .el-form-item__label {
        text-align: left;
      }
      &.required-not {
        margin-left: 10px;
        .el-form-item__label {
          width: 170px!important;
          text-align: left;
          padding-right: 0;
        }
        .el-form-item__content {
          margin-left: 170px!important;
        }
      }
    }
  }
}
</style>
